package negModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ListaVehiculos extends ArrayList{
	
	private static final long serialVersionUID = 1L;

	public ListaVehiculos (){
		super ();
	}
	
	public void RecuperarXml (Element xmlElement){
				
		Fabrica unaFabrica=new Fabrica();
		
		NodeList nodos=xmlElement.getElementsByTagName("vehiculo");
		for(int nc=0;nc<nodos.getLength();nc++){
			Vehiculo unaVe= unaFabrica.hacerVehiculo(((Element)nodos.item(nc)).getAttribute("Clase"),(Element)nodos.item(nc));
			this.add(unaVe);
			}	
	
	}
	
	public Element toXml(Document doc, String s){
		
		Element xmlElement = doc.createElement(s+"listaVehiculos");
		
		Iterator it = this.iterator();
	
		while (it.hasNext()) {
			Vehiculo unVehiculo=(Vehiculo)it.next() ;
			xmlElement.appendChild(unVehiculo.toXml(doc));
			}
		
		return xmlElement;
	}

	public boolean add(Object arg0) {
		
		if (arg0 instanceof Vehiculo){
			super.add(arg0);
			return true;
		}
			
		return false;
	}
	
	public void add(int arg0, Object arg1) {
		
		if (arg1 instanceof Vehiculo){
			super.add(arg0,arg1);
		}
		
	}
	
	private boolean tieneVehiculos(Collection arg0){
		
		Iterator iter = arg0.iterator();
		boolean esVehiculo = true;
		Object unVehiculo;
		
		while ((iter.hasNext()) && (esVehiculo)){
			unVehiculo = iter.next();
			if (!(unVehiculo instanceof Vehiculo))
				esVehiculo = false;
		}
		
		return esVehiculo;
	}
	
	public boolean addAll(Collection arg0) {
		
		boolean tiene = tieneVehiculos(arg0);
		
		if (tiene) 
			super.addAll(arg0);
		
		return tiene;
			
	}
	
}


